package com.itic.mall.system.user.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.mall.system.phonecode.persistence.dao.PhoneCodeDao;
import com.itic.mall.system.phonecode.persistence.model.PhoneCode;
import com.itic.mall.system.phonecode.persistence.model.PhoneCodeExample;
import com.itic.mall.system.tixian.persistence.dao.TixianDao;
import com.itic.mall.system.tixian.persistence.model.Tixian;
import com.itic.mall.system.user.persistence.dao.MallUserDao;
import com.itic.mall.system.user.persistence.model.MallUser;
import com.itic.mall.system.user.persistence.model.MallUserExample;


@Service
@Transactional
public class MallUserService
     extends CrudService<MallUserDao,MallUser,MallUserExample> {
    @Autowired
    private MallUserDao mallUserDao;
    @Autowired
    private PhoneCodeDao phoneCoderDao;
    @Autowired
    private TixianDao tixianDao;
    

	public MallUser getByUnionId(String unionId) {
		return mallUserDao.getByUnionId(unionId);
	}

	public MallUser getByOpenid(String openid) {
		return mallUserDao.getByOpenid(openid);
	}

	public String bindPhoneAndDelPhoneCode(MallUser user, String code) {
		
		//删除验证码
		PhoneCodeExample example = new PhoneCodeExample();
		example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andCodeEqualTo(code);
		PhoneCode phoneCode = new PhoneCode();
		phoneCode.setIsEffective(DBConst.FALSE);
		phoneCoderDao.updateByExampleSelective(phoneCode, example);
		
		mallUserDao.updateByPrimaryKeySelective(user);
		return SUCCESS;
	}

	public String tixianCreate(MallUser user, Tixian tixian) {
		tixianDao.insertSelective(tixian);
		mallUserDao.updateByPrimaryKeySelective(user);
		return SUCCESS;
	}

	public MallUser getByName(String userNickName) {
		return mallUserDao.getByName(userNickName);
	}


}